package com.hk.core.authentication.api.menus;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hk.commons.util.StringUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author kevin
 * @date 2019-12-27 11:31
 */
@Getter
@Setter
@Accessors(chain = true)
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Menu {

    /**
     * 浏览器总在一个新打开、未命名的窗口中载入目标文档
     */
    public static final String BLANK = "_blank";

    /**
     * 默认目标,浏览器在当前窗口打开链接
     */
    public static final String SELF = "_self";

    /**
     * 菜单id
     */
    private Long id;

    /**
     * 菜单名称
     */
    private String menuName;

    /**
     * 菜单组件名称，对于折叠类型的父类菜单，可以不配置值
     */
    private String component;

    /**
     * 菜单Icon
     */
    private String icon;

    /**
     * 菜单 链接
     */
    private String link;

    /**
     * target
     */
    private String target;

//    private Meta meta;
//
//    @Data
//    @Accessors(chain = true)
//    @JsonInclude(JsonInclude.Include.NON_NULL)
//    public static class Meta implements Serializable {
//
//        private String title;
//
//        /**
//         * 要打开的折叠菜单Key
//         */
//        private Set<Long> openKeys;
//
//        /**
//         * 要选中的菜单key
//         */
//        private Set<Long> selectedKeys;
//    }

    /**
     * 菜单需要的角色
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Set<Role> roles;

    /**
     * 菜单需要的权限
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private Set<Perm> perms;

    public String getTarget() {
        return StringUtils.defaultIfEmpty(target, SELF);
    }

    /**
     * 子级菜单
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<Menu> children;

    @Getter
    @Setter
    @Accessors(chain = true)
    public static class Role {

        private String value;

        @Override
        public int hashCode() {
            return value.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return obj instanceof Role role && Objects.equals(role.value, this.value);
        }
    }

    @Getter
    @Setter
    @Accessors(chain = true)
    public static class Perm {

        private String value;

        @Override
        public int hashCode() {
            return value.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return obj instanceof Perm perm && Objects.equals(perm.value, this.value);
        }
    }
}
